﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using rock.UI.common;

namespace rock.DAL
{
	/// <summary>
	/// 数据访问类:DalHOUSE_ROOM
	/// </summary>
	public partial class DalHOUSE_ROOM
	{
		public DalHOUSE_ROOM()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("SEQID", "HOUSE_ROOM"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int SEQID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from HOUSE_ROOM");
			strSql.Append(" where SEQID=@SEQID");
			SqlParameter[] parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4)
			};
			parameters[0].Value = SEQID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(rock.Model.ModelHOUSE_ROOM model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into HOUSE_ROOM(");
			strSql.Append("HOUSE_NUMBER,HOUSE_CODE,HOUSE_DONG_SEQID,CHAOXIANG,ROOM_TYPE,ROOM_FLOOR,STATUS,HOUSE_OWNER_SEQID,ROOM_SIZE,NOTE)");
			strSql.Append(" values (");
            strSql.Append("@HOUSE_NUMBER,@HOUSE_CODE,@HOUSE_DONG_SEQID,@CHAOXIANG,@ROOM_TYPE,@ROOM_FLOOR,@STATUS,@HOUSE_OWNER_SEQID,@ROOM_SIZE,@NOTE)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@HOUSE_NUMBER", SqlDbType.VarChar,20),
					new SqlParameter("@HOUSE_CODE", SqlDbType.VarChar,50),
					new SqlParameter("@HOUSE_DONG_SEQID", SqlDbType.Int,4),
					new SqlParameter("@CHAOXIANG", SqlDbType.VarChar,50),
					new SqlParameter("@ROOM_TYPE", SqlDbType.VarChar,50),
					new SqlParameter("@ROOM_FLOOR", SqlDbType.Int,4),
					new SqlParameter("@STATUS", SqlDbType.Int,4),
					new SqlParameter("@HOUSE_OWNER_SEQID", SqlDbType.Int,4),
					new SqlParameter("@ROOM_SIZE", SqlDbType.Decimal,9),
					new SqlParameter("@NOTE", SqlDbType.VarChar,300)};
			parameters[0].Value = model.HOUSE_NUMBER;
            parameters[1].Value = model.HOUSE_CODE;
			parameters[2].Value = model.HOUSE_DONG_SEQID;
			parameters[3].Value = model.CHAOXIANG;
			parameters[4].Value = model.ROOM_TYPE;
			parameters[5].Value = model.ROOM_FLOOR;
			parameters[6].Value = model.STATUS;
			parameters[7].Value = model.HOUSE_OWNER_SEQID;
			parameters[8].Value = model.ROOM_SIZE;
			parameters[9].Value = model.NOTE;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(rock.Model.ModelHOUSE_ROOM model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update HOUSE_ROOM set ");
			strSql.Append("HOUSE_NUMBER=@HOUSE_NUMBER,");
            strSql.Append("HOUSE_CODE=@HOUSE_CODE,");
			strSql.Append("HOUSE_DONG_SEQID=@HOUSE_DONG_SEQID,");
			strSql.Append("CHAOXIANG=@CHAOXIANG,");
			strSql.Append("ROOM_TYPE=@ROOM_TYPE,");
			strSql.Append("ROOM_FLOOR=@ROOM_FLOOR,");
			strSql.Append("STATUS=@STATUS,");
			strSql.Append("HOUSE_OWNER_SEQID=@HOUSE_OWNER_SEQID,");
			strSql.Append("ROOM_SIZE=@ROOM_SIZE,");
			strSql.Append("NOTE=@NOTE");
			strSql.Append(" where SEQID=@SEQID");
			SqlParameter[] parameters = {
					new SqlParameter("@HOUSE_NUMBER", SqlDbType.VarChar,20),
					new SqlParameter("@HOUSE_CODE", SqlDbType.VarChar,50),
					new SqlParameter("@HOUSE_DONG_SEQID", SqlDbType.Int,4),
					new SqlParameter("@CHAOXIANG", SqlDbType.VarChar,50),
					new SqlParameter("@ROOM_TYPE", SqlDbType.VarChar,50),
					new SqlParameter("@ROOM_FLOOR", SqlDbType.Int,4),
					new SqlParameter("@STATUS", SqlDbType.Int,4),
					new SqlParameter("@HOUSE_OWNER_SEQID", SqlDbType.Int,4),
					new SqlParameter("@ROOM_SIZE", SqlDbType.Decimal,9),
					new SqlParameter("@NOTE", SqlDbType.VarChar,300),
					new SqlParameter("@SEQID", SqlDbType.Int,4)};
			parameters[0].Value = model.HOUSE_NUMBER;
            parameters[1].Value = model.HOUSE_CODE;
			parameters[2].Value = model.HOUSE_DONG_SEQID;
			parameters[3].Value = model.CHAOXIANG;
			parameters[4].Value = model.ROOM_TYPE;
			parameters[5].Value = model.ROOM_FLOOR;
			parameters[6].Value = model.STATUS;
			parameters[7].Value = model.HOUSE_OWNER_SEQID;
			parameters[8].Value = model.ROOM_SIZE;
			parameters[9].Value = model.NOTE;
			parameters[10].Value = model.SEQID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int SEQID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from HOUSE_ROOM ");
			strSql.Append(" where SEQID=@SEQID");
			SqlParameter[] parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4)
			};
			parameters[0].Value = SEQID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string SEQIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from HOUSE_ROOM ");
			strSql.Append(" where SEQID in ("+SEQIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


        public DataSet  getHouseRoomInfo(int seqid){
            String sql = @"SELECT 
            HOUSE_ROOM.SEQID,
            HOUSE_ROOM.HOUSE_NUMBER AS HOUSE_NAME , 
            HOUSE_DONG.NAME AS DONG_NAME , 
            HOUSE_XIAOQU.NAME AS XIAOQU_NAME , 
            HOUSE_ROOM.ROOM_SIZE AS ROOM_SIZE , 
            ISNULL(HOUSE_ROOM.ROOM_FLOOR , 0) AS ROOM_FLOOR, 
            ISNULL(HOUSE_DONG.ALL_FLOOR_END , 0) AS ALL_FLOOR_END, 
            ISNULL( HOUSE_DONG.HAS_DIANTI , 0) AS HAS_DIANTI
            FROM HOUSE_ROOM
            LEFT JOIN HOUSE_DONG ON HOUSE_ROOM.HOUSE_DONG_SEQID = HOUSE_DONG.SEQID
            LEFT JOIN HOUSE_XIAOQU ON HOUSE_DONG.HOUSE_XIAOQU_SEQID = HOUSE_XIAOQU.SEQID
            WHERE HOUSE_ROOM.SEQID = @SEQID";
            SqlParameter[] parameters = {
					            new SqlParameter("@SEQID", SqlDbType.Int,4)
			            };
			parameters[0].Value = seqid;

             return DbHelperSQL.Query(sql , parameters);

        }


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public rock.Model.ModelHOUSE_ROOM GetModel(int SEQID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 SEQID,HOUSE_NUMBER,HOUSE_CODE,HOUSE_DONG_SEQID,CHAOXIANG,ROOM_TYPE,ROOM_FLOOR,STATUS,HOUSE_OWNER_SEQID,ROOM_SIZE,NOTE from HOUSE_ROOM ");
			strSql.Append(" where SEQID=@SEQID");
			SqlParameter[] parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4)
			};
			parameters[0].Value = SEQID;

			rock.Model.ModelHOUSE_ROOM model=new rock.Model.ModelHOUSE_ROOM();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["SEQID"]!=null && ds.Tables[0].Rows[0]["SEQID"].ToString()!="")
				{
					model.SEQID=int.Parse(ds.Tables[0].Rows[0]["SEQID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["HOUSE_NUMBER"]!=null && ds.Tables[0].Rows[0]["HOUSE_NUMBER"].ToString()!="")
				{
					model.HOUSE_NUMBER=ds.Tables[0].Rows[0]["HOUSE_NUMBER"].ToString();
				}
                if (ds.Tables[0].Rows[0]["HOUSE_CODE"] != null && ds.Tables[0].Rows[0]["HOUSE_CODE"].ToString() != "")
				{
                    model.HOUSE_CODE = ds.Tables[0].Rows[0]["HOUSE_CODE"].ToString();
				}
				if(ds.Tables[0].Rows[0]["HOUSE_DONG_SEQID"]!=null && ds.Tables[0].Rows[0]["HOUSE_DONG_SEQID"].ToString()!="")
				{
					model.HOUSE_DONG_SEQID=int.Parse(ds.Tables[0].Rows[0]["HOUSE_DONG_SEQID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CHAOXIANG"]!=null && ds.Tables[0].Rows[0]["CHAOXIANG"].ToString()!="")
				{
					model.CHAOXIANG=ds.Tables[0].Rows[0]["CHAOXIANG"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ROOM_TYPE"]!=null && ds.Tables[0].Rows[0]["ROOM_TYPE"].ToString()!="")
				{
					model.ROOM_TYPE=ds.Tables[0].Rows[0]["ROOM_TYPE"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ROOM_FLOOR"]!=null && ds.Tables[0].Rows[0]["ROOM_FLOOR"].ToString()!="")
				{
					model.ROOM_FLOOR=int.Parse(ds.Tables[0].Rows[0]["ROOM_FLOOR"].ToString());
				}
				if(ds.Tables[0].Rows[0]["STATUS"]!=null && ds.Tables[0].Rows[0]["STATUS"].ToString()!="")
				{
					model.STATUS=int.Parse(ds.Tables[0].Rows[0]["STATUS"].ToString());
				}
				if(ds.Tables[0].Rows[0]["HOUSE_OWNER_SEQID"]!=null && ds.Tables[0].Rows[0]["HOUSE_OWNER_SEQID"].ToString()!="")
				{
					model.HOUSE_OWNER_SEQID=int.Parse(ds.Tables[0].Rows[0]["HOUSE_OWNER_SEQID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ROOM_SIZE"]!=null && ds.Tables[0].Rows[0]["ROOM_SIZE"].ToString()!="")
				{
					model.ROOM_SIZE=decimal.Parse(ds.Tables[0].Rows[0]["ROOM_SIZE"].ToString());
				}
				if(ds.Tables[0].Rows[0]["NOTE"]!=null && ds.Tables[0].Rows[0]["NOTE"].ToString()!="")
				{
					model.NOTE=ds.Tables[0].Rows[0]["NOTE"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList( string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");

            strSql.Append(" SEQID,HOUSE_NUMBER,HOUSE_CODE,HOUSE_DONG_SEQID,CHAOXIANG,ROOM_TYPE,ROOM_FLOOR,STATUS,HOUSE_OWNER_SEQID,ROOM_SIZE,NOTE ");
            strSql.Append(" FROM HOUSE_ROOM ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
          
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" SEQID,HOUSE_NUMBER,HOUSE_CODE,HOUSE_DONG_SEQID,CHAOXIANG,ROOM_TYPE,ROOM_FLOOR,STATUS,HOUSE_OWNER_SEQID,ROOM_SIZE,NOTE ");
			strSql.Append(" FROM HOUSE_ROOM ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
 
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
        public PageDataSet GetListByPage( String where, string orderby, int pageNo, int pageSize)
		{
            String sql = @" SELECT 
                            HOUSE_ROOM.SEQID,
                            HOUSE_ROOM.HOUSE_NUMBER,
                            HOUSE_ROOM.HOUSE_CODE,
                            HOUSE_ROOM.CHAOXIANG,
                            HOUSE_ROOM.ROOM_TYPE,
                            HOUSE_ROOM.ROOM_FLOOR,
                            HOUSE_ROOM.STATUS,
                            HOUSE_ROOM.ROOM_SIZE,
                            HOUSE_OWNER.SEQID AS OWNER_SEQID , 
                            HOUSE_OWNER.NAME AS OWNER_NAME ,
                            TEMP.XIAOQU_NAME ,
                            TEMP.DONG_NAME,
                            TEMP.ALL_FLOOR_START,
                            TEMP.ALL_FLOOR_END,
                            TEMP.HAS_DIANTI,
							RENT_CUSTOM.NAME as CUSTOM_NAME
                            FROM HOUSE_ROOM 
                            LEFT JOIN HOUSE_OWNER ON HOUSE_ROOM.HOUSE_OWNER_SEQID = HOUSE_OWNER.SEQID
                            LEFT JOIN  
                            (
                            SELECT  HOUSE_XIAOQU.NAME as XIAOQU_NAME , HOUSE_DONG.NAME AS DONG_NAME, ALL_FLOOR_START,ALL_FLOOR_END , HAS_DIANTI ,
                            HOUSE_XIAOQU.SEQID  as XIAOQU_SEQID , HOUSE_DONG.SEQID  as DONG_SEQID 
                            FROM HOUSE_XIAOQU , HOUSE_DONG WHERE HOUSE_XIAOQU.SEQID = HOUSE_DONG.HOUSE_XIAOQU_SEQID
                            ) AS TEMP ON HOUSE_ROOM.HOUSE_DONG_SEQID = TEMP.DONG_SEQID 
							LEFT JOIN RENT_CONTRACT ON RENT_CONTRACT.HOUSE_ROOM_SEQID = HOUSE_ROOM.SEQID AND RENT_CONTRACT.STATUS = 1
							LEFT JOIN RENT_CUSTOM   ON RENT_CONTRACT.RENT_CUSTOM_SEQID = RENT_CUSTOM.SEQID  ";
            if (where != null && !where.Equals("")) {
                sql += " WHERE " + where;
            }

            PageDataSet pds = new PageDataSet();
            pds.AllRecord = DbHelperSQL.GetRecordCount(sql);
            pds.DataSet = DbHelperSQL.GetListByPage(sql, orderby, pageNo, pageSize);

            return pds;
			 
		}

		 

		#endregion  Method
	}
}

